#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include "OLED.h"
#include "DHT11.h"
#include "BH1750.h"
#include "Beep.h"
#include "LED.h"

char OLED_Buf[20];					//sprintf格式化打印缓冲数组
extern DHT11_Humi_Temp DHT11_Data;	//温湿度结构体变量
extern uint8_t Alarm_Flag;			//报警标志位

/**
  * @brief	定时器2初始化函数
  * @param	无
  * @retval	无
  */
void Timer2_Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = 10000-1;
	TIM_TimeBaseInitStructure.TIM_Prescaler = 7200-1;
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_Init(&NVIC_InitStructure);
	
	TIM_Cmd(TIM2, ENABLE);
}

/**
  * @brief	定时器3初始化函数
  * @param	无
  * @retval	无
  */
void Timer3_Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = 2500-1;
	TIM_TimeBaseInitStructure.TIM_Prescaler = 7200-1;
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_Init(&NVIC_InitStructure);
	
	TIM_Cmd(TIM3, ENABLE);
}

/**
  * @brief	定时器2中断函数
  * @param	无
  * @retval	无
  */
void TIM2_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
	{
		OLED_ShowString(1,5,"Wellcom");
		
		sprintf(OLED_Buf, "Humi:%d.%d", DHT11_Data.Humi_Int, DHT11_Data.Humi_Dec);
		OLED_ShowString(2,1,OLED_Buf);
		
		sprintf(OLED_Buf, "Temp:%d.%d", DHT11_Data.Temp_Int, DHT11_Data.Temp_Int);
		OLED_ShowString(3,1,OLED_Buf);
		
		sprintf(OLED_Buf, "Ligh:%d.%d", (uint16_t)temp1, (uint16_t)temp2);
		OLED_ShowString(4,1,OLED_Buf);
		
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
	}
}

/**
  * @brief	定时器3中断函数
  * @param	无
  * @retval	无
  */
void TIM3_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
	{
		if(Alarm_Flag == 1)
		{
			Beep_Turn();
			Led1_Turn();
		}
		else
		{
			Beep_Off();
			Led1_Off();
		}
		
		TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
	}
}
